<template>
  <div class="container">
    <!-- 导航条 -->
    <van-nav-bar left-arrow @click-left="$router.back()" title="编辑资料">
    </van-nav-bar>

    <!-- 编辑区 -->
    <van-cell-group>
      <van-cell is-link title="头像" center @click="hClickImage">
      <input type="file" hidden ref="refInput" @change="hImageChange" />
        <van-image
          slot="default"
          width="1.5rem"
          height="1.5rem"
          fit="cover"
          round
          :src="userInfo.photo"
        />
      </van-cell>
      <!-- value： 设置右侧显示的文字 -->
      <van-cell
        is-link
        title="姓名"
        :value="userInfo.name"
        @click="hEditName"
      />

      <van-cell
        is-link
        title="性别"
        :value="userInfo.gender === 1 ? '男' : '女'"
        @click="isShowGender = true"
      />

      <van-cell
        is-link
        title="生日"
        :value="userInfo.birthday"
        @click="isShowBirthday = true"
      />

      <!-- 修改名字  -->
      <van-dialog
        v-model="isShowName"
        title="修改名字"
        show-cancel-button
        @confirm="hSaveName"
      >
        <van-field v-model.trim="newName" />
      </van-dialog>

      <!-- 修改性别 -->
      <van-popup v-model="isShowGender" position="bottom">
        <van-nav-bar
          title="修改性别"
          left-text="取消"
          @click-left="showGender = false"
        >
        </van-nav-bar>
        <van-cell title="男" @click="hSaveGender(1)" is-link></van-cell>
        <van-cell title="女" @click="hSaveGender(0)" is-link></van-cell>
      </van-popup>

      <!-- 修改生日 -->
      <van-popup v-model="isShowBirthday" position="bottom">
        <van-nav-bar title="修改生日">
        </van-nav-bar>
        <van-datetime-picker
          v-model="newDate"
          type="date"
          title="选择年月日"
          @cancel="showBirthday=false"
          @confirm="hSaveBirthday"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { updateUserInfo, updateUserPhoto } from '@/api/user.js'
import dayjs from 'dayjs'

export default {
  name: 'userProfile',
  data () {
    return {
      // 控制弹层
      isShowName: false,
      isShowGender: false,
      isShowBirthday: false,

      // 修改后的新名字
      newName: '',
      // 修改后新生日
      newDate: new Date(),
      minDate: new Date(1965, 0, 10), // dateTime-picker中最小时间
      maxDate: new Date() // 当前时间
    }
  },
  computed: {
    ...mapState(['userInfo'])
  },
  methods: {
    hEditName () {
      this.newName = this.userInfo.name
      this.isShowName = true
    },
    hClickImage () {
      this.$refs.refInput.click()
    },
    async hSaveName () {
      // 调用接口，保存名字
      const { newName } = this
      if (newName === '') {
        return
      }
      try {
        const res = await updateUserInfo({ name: newName })
        console.log(res)
        // 成功
        // 1. 更新数据到vuex
        this.$store.commit('mUpdateName', newName)
      } catch (error) {
        console.log(error)
      }
    },
    async hSaveGender (gender) {
      try {
        const result = await updateUserInfo({ gender })
        console.log(result)
        // 在本地更新vuex
        this.$store.commit('mUpdateGender', gender)
        this.$toast.success('操作成功')
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
    },
    async hSaveBirthday () {
      // console.log(this.newDate)
      const birthday = dayjs(this.newDate).format('YYYY-MM-DD')
      try {
        const res = await updateUserInfo({ birthday })
        console.log(res)
        // 成功
        // 1. 更新数据到vuex
        this.$store.commit('mUpdateBirthday', birthday)
        // 2. 隐藏弹层
        this.isShowBirthday = false

        this.$toast.success('修改生日成功')
      } catch (error) {
        console.log(error)
      }
    },
    async hImageChange (e) {
      console.log(e)
      // 获取用户当前选中的图片
      const file = e.target.files[0]
      // 没有选中图片，返回
      if (!file) {
        return
      }
      console.log('当前用户选中的图片文件是', file)
      // 调用接口，上传这张图片
      const fd = new FormData()
      fd.append('photo', file)
      const { data } = await updateUserPhoto(fd)
      console.log(data)
      // 保存结果
      this.$store.commit('mUpdatePhoto', data.photo)
      this.$toast.success('头像修改成功')
    }
  }
}
</script>
